package com.kitty.game.gm.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.child.model.ChildInfo;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.FieldUtils;
import com.kitty.mina.message.MessagePusher;

import java.util.List;

public class GmLevel implements GMFather {
    @Override
    public boolean handle(Role role, String content) {
        int level = Integer.parseInt(content);
//        if (level > 110){
//            MessagePusher.pushMessage(role, new RespNotifyMiscEx("等级不能超过110级！"));
//            return true;
//        }
        if (level > (role.isRealBody() ? 125 : 179)) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("等级不在范围之内！"));
            return true;
        }
        ChildInfo childInfo = role.getChildInfo();
        int currlevel = role.isRealBody() ? role.getLevel() : childInfo.getLevel();
        for (int i = currlevel; i <= level; i++) {
            SpringUtils.getRoleService().addExp(role, Integer.MAX_VALUE, role.getLevel(), role.getPetBox().getFightPetId());
        }
        if (role.isRealBody()) {
            role.setLevel((short) level);
        } else {
            childInfo.setLevel((short) level);
        }
        List<FiedValue> values = FieldUtils.rolePanelInfo(role);
        RespUpdate respUpdate = new RespUpdate();
        respUpdate.setRoleId(role.getRoleId());
        respUpdate.setList(values);
        MessagePusher.pushMessage(role, respUpdate);

        Pet pet = role.getPetBox().getPetByPetId(role.getPetBox().getFightPetId());
        if (pet != null) {
            pet.setLevel((short) (Math.min(level, 125)));
            SpringUtils.getPetService().refreshPetInfo(role, pet);
        }
        return true;
    }
}
